//
//  XMMineView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/9.
//

#import "XMMineView.h"
#import "XMMineHeaderView.h"
#import "XMMineHomeCell.h"
#import "XMTopTabSelectView.h"

@interface XMMineView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, XMMineHeaderViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) XMTopTabSelectView *topTabSelectView;

@property (nonatomic, strong) XMUserInfoModel *userInfoModel;

@property (nonatomic, strong) NSMutableArray *productModelArr;

@end

@implementation XMMineView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, KScaleWidth(612.0))];
    [titleView setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0x7A6A3F).CGColor, (__bridge id)kColorHEX(0x817A66).CGColor] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1)];
    [self addSubview:titleView];
    [self addSubview:self.collectionView];
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, kNavagationBarH)];
    topView.backgroundColor = kColorHEX(0x7A6A3F);
    [self addSubview:topView];
    
    UIButton *addFriendBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(KScaleWidth(10.0), KScaleWidth(43.0), KScaleWidth(32.0), KScaleWidth(32.0)) image:@"mine_add_friend_icon" selectImg:@"mine_add_friend_icon"];
    [addFriendBtn addTarget:self action:@selector(addFriendAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:addFriendBtn];
    {
        UIButton *scanBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(self.width - KScaleWidth(93.0) - KScaleWidth(18.0) - KScaleWidth(5.0), KScaleWidth(48.0), KScaleWidth(28.0), KScaleWidth(28.0)) image:@"mine_scan_icon" selectImg:@"mine_scan_icon"];
        [scanBtn addTarget:self action:@selector(scanAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:scanBtn];
        
        UIButton *visitorBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(scanBtn.maxX + KScaleWidth(8.0), scanBtn.y, KScaleWidth(28.0), KScaleWidth(28.0)) image:@"mine_visitor_icon" selectImg:@"mine_visitor_icon"];
        [visitorBtn addTarget:self action:@selector(visitorCheckBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:visitorBtn];
        
        UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(visitorBtn.maxX + KScaleWidth(8.0), visitorBtn.y, KScaleWidth(28.0), KScaleWidth(28.0)) image:@"mine_set_icon" selectImg:@"mine_set_icon"];
        [setBtn addTarget:self action:@selector(setAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:setBtn];
    }
    [self addSubview:self.topTabSelectView];
}

//添加朋友
- (void)addFriendAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(addFriend)]) {
        [self.delegate addFriend];
    }
}

//扫一扫
- (void)scanAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(scanAction)]) {
        [self.delegate scanAction];
    }
}

//设置
- (void)setAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(setAction)]) {
        [self.delegate setAction];
    }
}

//访客查看
- (void)visitorCheckBtn:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkAccessRecord)]) {
        [self.delegate checkAccessRecord];
    }
}

//加载接口数据
- (void)setContentWithUserInfoModel:(XMUserInfoModel *)userInfoModel {
    _userInfoModel = userInfoModel;
    [_collectionView reloadData];
}

//加载作品列表
- (void)setProductListWithProductModelArr:(NSArray *)productModelArr
                                     page:(NSInteger)page {
    [_collectionView.mj_header endRefreshing];
    if (page == 1) {
        [self.productModelArr removeAllObjects];
    }
    if (productModelArr.count < 10) {
        [_collectionView.mj_footer endRefreshingWithNoMoreData];
    }
    else {
        [_collectionView.mj_footer endRefreshing];
    }
    [self.productModelArr addObjectsFromArray:productModelArr];
    [_collectionView reloadData];
}

#pragma mark - CollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.productModelArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(self.width, KScaleWidth(550.0));
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        XMMineHeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XMMineHomeHeadViewName" forIndexPath:indexPath];
        [headView resetInfoWithUserInfoModel:_userInfoModel];
        headView.delegate = self;
        return headView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat itemWidth = floor((self.width - KScaleWidth(8.0)*4)/3);
    return CGSizeMake(itemWidth, KScaleWidth(154.0));
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMMineHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XMMineHomeCellName" forIndexPath:indexPath];
    [cell setContentWithProduction:_productModelArr[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkProductDetailWithProductId:)]) {
        XMProductModel *productModel = _productModelArr[indexPath.row];
        [self.delegate checkProductDetailWithProductId:productModel.objectId];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _topTabSelectView.y = MAX(KScaleWidth(500) - scrollView.contentOffset.y, kNavagationBarH);
}

#pragma mark - Delegate
- (void)homePageEditAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(homePageEdit)]) {
        [self.delegate homePageEdit];
    }
}

- (void)checkEarnDetail {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkEarnDetailAction)]) {
        [self.delegate checkEarnDetailAction];
    }
}

- (void)putForwardMoney {
    if (self.delegate && [self.delegate respondsToSelector:@selector(putForwardMoneyAction)]) {
        [self.delegate putForwardMoneyAction];
    }
}

- (void)checkMyOrderListWithOrderType:(NSInteger)orderType {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkMyOrderListActionWithOrderType:)]) {
        [self.delegate checkMyOrderListActionWithOrderType:orderType];
    }
}

//实名认证
- (void)authenAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(authenAction)]) {
        [self.delegate authenAction];
    }
}

//查看二维码
- (void)qrcodeCheckAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(qrcodeCheckAction)]) {
        [self.delegate qrcodeCheckAction];
    }
}

#pragma mark - Init
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemWidth = floor((self.width - KScaleWidth(8.0)*4)/3);
        layout.itemSize = CGSizeMake(itemWidth, KScaleWidth(154.0));
        layout.minimumInteritemSpacing = KScaleWidth(8.0);
        layout.minimumLineSpacing = KScaleWidth(8.0);
        layout.sectionInset = UIEdgeInsetsMake(KScaleWidth(8.0), KScaleWidth(8.0), KScaleWidth(8.0), KScaleWidth(8.0));
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = kColorHEX(0xF5F5F5);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[XMMineHomeCell class] forCellWithReuseIdentifier:@"XMMineHomeCellName"];
        [_collectionView registerClass:[XMMineHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XMMineHomeHeadViewName"];
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
        XMWeakSelf;
        _collectionView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshData)]) {
                [weakSelf.delegate refreshData];
            }
        }];
        
        _collectionView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(loadMoreData)]) {
                [weakSelf.delegate loadMoreData];
            }
        }];
    }
    return _collectionView;
}

- (XMTopTabSelectView *)topTabSelectView {
    if (!_topTabSelectView) {
        _topTabSelectView = [[XMTopTabSelectView alloc] initWithFrame:CGRectMake(0, KScaleWidth(500.0), self.width, KScaleWidth(50.0))];
        XMWeakSelf;
        _topTabSelectView.itemSelectBlock = ^(NSInteger selectIndex) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(listTypeSelect:)]) {
                [weakSelf.delegate listTypeSelect:selectIndex];
            }
        };
    }
    return _topTabSelectView;
}

- (NSMutableArray *)productModelArr {
    if (!_productModelArr) {
        _productModelArr = [NSMutableArray array];
    }
    return _productModelArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
